import { message } from "@/utils/message";
import {
  queryElevatorList,
  deleteElevator,
  querySysUserListByBm
} from "@/api/sysele";
import { type PaginationProps } from "@pureadmin/table";
import { reactive, ref, computed, onMounted } from "vue";

export function useEle() {
  const INITIAL_DATA = {
    id: "",
    manageid: "",
    name: "",
    tung: "",
    unit: "",
    elevatorid: "",
    createtime: "",
    qrcode: "",
    aeskey: "",
    maxfloor: "",
    qrcodeurl: ""
  };

  const searchForm = reactive({
    manageid: "",
    tung: "",
    elevatorid: ""
  });
  const userDialogVisible = ref(false);
  const userFormData = ref({ ...INITIAL_DATA });

  const qrCodeDialogVisible = ref(false);

  const bmData = ref([]);

  const dataList = ref([]);
  const loading = ref(true);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });

  const columns: TableColumnList = [
    {
      type: "selection",
      width: 55,
      align: "left",
      hide: ({ checkList }) => !checkList.includes("勾选列")
    },
    {
      label: "序号",
      type: "index",
      width: 70,
      hide: ({ checkList }) => !checkList.includes("序号列")
    },
    {
      label: "编号",
      prop: "id",
      minWidth: 130
    },
    {
      label: "小区名称",
      prop: "manageid",
      minWidth: 130,
      formatter: row => {
        const option = bmData.value.find(item => item.id === row.manageid);
        return option ? option.fullname : "";
      }
    },
    {
      label: "电梯",
      prop: "tung",
      minWidth: 130,
      formatter: row => {
        return row.tung + "栋" + row.unit + "单元";
      }
    },
    {
      label: "电梯号",
      prop: "name",
      minWidth: 130
    },
    {
      label: "最高楼层",
      prop: "maxfloor",
      minWidth: 80
    },
    {
      label: "编码",
      prop: "elevatorid",
      minWidth: 130
    },
    {
      label: "创建时间",
      minWidth: 90,
      prop: "createtime"
    },
    {
      label: "操作",
      fixed: "right",
      width: 180,
      slot: "operation"
    }
  ];
  const buttonClass = computed(() => {
    return [
      "!h-[20px]",
      "reset-margin",
      "!text-gray-500",
      "dark:!text-white",
      "dark:hover:!text-primary"
    ];
  });

  function handleAdd() {
    userDialogVisible.value = true;
    userFormData.value = { ...INITIAL_DATA };
  }

  function handleUpdate(row) {
    userDialogVisible.value = true;
    userFormData.value = { ...row };
  }

  function handleQrCode(row) {
    qrCodeDialogVisible.value = true;
    userFormData.value = { ...row };
  }

  function handleDelete(row) {
    deleteElevator(row)
      .then(res => {
        // console.log(JSON.stringify(res));
        if (res.success) {
          message("删除成功", { type: "success" });
          onSearch();
        } else {
          message("删除数据异常:" + res.errMsg, { type: "error" });
        }
      })
      .catch(error => {
        message("删除数据异常:" + JSON.stringify(error), { type: "error" });
      });
  }

  function handleSizeChange(val: number) {
    console.log(`${val} items per page`);
  }

  function handleCurrentChange(val: number) {
    console.log(`current page: ${val}`);
    onSearch();
  }

  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
  }

  async function onSearch() {
    loading.value = true;
    queryElevatorList({
      manageid: searchForm.manageid,
      tung: searchForm.tung,
      elevatorid: searchForm.elevatorid,
      pageSize: pagination.pageSize,
      currentPage: pagination.currentPage
    })
      .then(res => {
        if (res.success) {
          dataList.value = res.data.list;
          pagination.total = res.data.total;
          loading.value = false;
        } else {
          message("查询数据异常:" + res.errMsg, { type: "error" });
        }
      })
      .catch(error => {
        message("查询数据异常:" + JSON.stringify(error), { type: "error" });
      });
  }

  async function queryBm() {
    querySysUserListByBm()
      .then(res => {
        if (res.success) {
          bmData.value = res.data.list;
        } else {
          bmData.value = [];
        }
      })
      .catch(error => {
        bmData.value = [];
        console.log(JSON.stringify(error));
      });
  }

  const resetForm = formEl => {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  };

  onMounted(() => {
    onSearch();
    queryBm();
  });

  return {
    bmData,
    qrCodeDialogVisible,
    userDialogVisible,
    userFormData,
    searchForm,
    loading,
    columns,
    dataList,
    pagination,
    buttonClass,
    onSearch,
    resetForm,
    handleAdd,
    handleUpdate,
    handleDelete,
    handleQrCode,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange
  };
}
